package com.wedata.weimob;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 健萌补券脚本 {

    public static void main(String[] args) throws IOException {
        //System.out.println(微盟分销业务.postRequestWithSign());
        Map<Integer,String> map = getCoachPhone();
        FileReader fileReader = new FileReader("src\\main\\java\\com\\wedata\\weimob\\健萌补券脚本.txt");
        BufferedReader br = new BufferedReader(fileReader);
        String line = br.readLine();
        try{
            for(;line!=null;line = br.readLine()){
                String[] ss = line.split("\t");
                String phone = map.get(Integer.valueOf(ss[0]));
                try{
                    JSONObject jjjj = new JSONObject(微盟分销业务.getUserInfo(phone,"")).getJSONObject("data");
                    Long phoneWid = jjjj.getLong("wid");
                    String code1 = getCouponList(ss[1]);
                    String code2 = getCouponList(phoneWid.toString());
                    if(code1!=null && code2==null){
                        cancelCoupon(ss[1],code1);
                        code2 = new JSONObject(微盟分销业务.receiveCoupon(phoneWid.toString())).getJSONObject("data").getJSONArray("couponResultList").getJSONObject(0).getJSONArray("codes").getStr(0);
                        System.out.println("已改\t"+ss[0]+"\t"+ss[1]+"\t"+code1+"\t"+phone+"\t"+code2);
                    }else{
                        System.out.println("未改\t"+ss[0]+"\t"+ss[1]+"\t"+code1+"\t"+phone+"\t"+code2);
                    }
                }catch (Exception ee){
                    System.out.println(phone+"手机号未注册，不发放优惠券");
                }

            }
        }catch (Exception ex){
            System.out.println(ex);
        }
        br.close();
        fileReader.close();
    }
    public static Map<Integer,String> getCoachPhone() throws IOException {
        FileReader fileReader = new FileReader("src\\main\\java\\com\\wedata\\weimob\\教练手机号.txt");
        BufferedReader br = new BufferedReader(fileReader);
        String line = br.readLine();
        Map<Integer,String> map = new HashMap<>();
        for(;line!=null;line = br.readLine()){
            String[] ss = line.split("\t");
            map.put(Integer.valueOf(ss[0]),ss[1]);
        }
        br.close();
        fileReader.close();
        return map;
    }
    //查询用户券列表
    public static String getCouponList(String wid){
        JSONObject body = new JSONObject();
        body.set("wid",wid);
        body.set("pageSize",10);
        body.set("pageNum",1);
        body.set("statusRange",new Integer[]{1,2});

        String resultStr = HttpUtil.post("https://dopen.weimob.com/apigw/weimob_crm/v2.0/coupon/customer/getList?accesstoken="+微盟分销业务.postRequestWithSign(), body.toString());
        //System.out.println(resultStr);
        JSONArray jsonArray = new JSONObject(resultStr).getJSONObject("data").getJSONArray("pageList");
        for (Integer integer =0; integer<jsonArray.size();integer++){
            Long couponTemplateId = jsonArray.getJSONObject(integer).getLong("couponTemplateId");
            if(couponTemplateId==66823917L){
                Integer status = jsonArray.getJSONObject(integer).getInt("status");
                //System.out.println(couponTemplateId+"状态：,"+status);
                return jsonArray.getJSONObject(integer).getStr("code");
            }
        }
        return null;
    }

    //作废优惠券
    public static void cancelCoupon(String wid,String code){
        JSONObject body = new JSONObject();
        body.set("wid",wid);
        body.set("vid","6001490628846");
        body.set("type",2);

        List operateCoupons = new ArrayList<>();
        JSONObject coupon = new JSONObject();
        coupon.set("couponTemplateId",66823917L);
        coupon.set("code",code);
        operateCoupons.add(coupon);
        body.set("operateCoupons",operateCoupons);

        String resultStr = HttpUtil.post("https://dopen.weimob.com/apigw/weimob_crm/v2.0/coupon/cancel?accesstoken="+微盟分销业务.postRequestWithSign(), body.toString());
        System.out.println(resultStr);
    }

}
